package theeg.DependencyAnalysis.Core.Algorithms.SubstringDetection;

import java.util.HashSet;

class CommonSubstringStatus {

    private final int _numberOfTexts;
    public Node node;
    private boolean _allStrings;
    private boolean _twoStrings;
    private final HashSet<Integer> _stringSet;
    public int height;

    public CommonSubstringStatus(Node node, int height, int numberOfTexts) {
        _numberOfTexts = numberOfTexts;
        this.node = node;
        _stringSet = new HashSet();

        this.height = height;
    }

    public boolean isInAtLeastTwoStrings() {
        if (!_twoStrings) {
            int hits = 0;
            for (int i = 0; i < _numberOfTexts; i++) {
                if (_stringSet.contains(i)) {
                    hits++;
                }
                if (hits > 1) {
                    _twoStrings = true;
                    break;
                }
            }
        }
        return _twoStrings;
    }

    public boolean isInAllStrings() {
        if (!_allStrings) {
            for (int i = 0; i < _numberOfTexts; i++) {
                if (!_stringSet.contains(i)) {
                    return false;
                }
            }
            _allStrings = true;
        }
        return _allStrings;
    }

    public void addString(int number) {
        if (!isInAllStrings()) {
            if (!_stringSet.contains(number)) {
                _stringSet.add(number);
            }
        }
    }

    public void mergeStatus(CommonSubstringStatus status) {
        _allStrings |= status._allStrings;
        if (!isInAllStrings()) {
            for (Integer i : status._stringSet) {
                _stringSet.add(i);
            }
        }
    }
}
